﻿#region copyright
//<copyright>
// Copyright(C) 2012 TrackerRealm Corporation
// This file is part of the open source project - Jazz. http://jazz.codeplex.com
// 
// Jazz is open software: you can redistribute it and/or modify it 
// under the terms of the GNU Affero General Public License (AGPL) as published by 
// the Free Software Foundation, version 3 of the License.
// 
// Jazz is distributed in the hope that it will be useful, 
// but WITHOUT ANY WARRANTY; without even the implied warranty 
// of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
//  See the GNU Affero General Public License (AGPL) for more details.
// 
// You should have received a copy of the GNU General Public 
// License along with Jazz.  If not, see <http://www.gnu.org/licenses/>.
//
// REMOVAL OF THIS NOTICE IS VIOLATION OF THE COPYRIGHT. 
//</copyright>
#endregion
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Reflection;
using TrackerRealm.Jazz.Common;
using C = TrackerRealm.Jazz.Common.JazzConstants;

namespace TrackerRealm.Jazz.Client
{
    /// <summary>
    /// jState is a wrapper class wrapping the state method's 'MethodInfo'.
    /// </summary>
    public class jState
    {
        /// <summary>
        /// 
        /// </summary>
        public static readonly jState[] EmptyArray = new jState[0];
        /// <summary>
        /// 
        /// </summary>
        public static readonly jState Empty = new jState() { Name = C.EMPTY };
        #region Factory methods - Create
        /// <summary>
        /// Create
        /// </summary>
        /// <param name="m"></param>
        /// <returns></returns>
        internal static jState Create(MethodInfo m)
        {
            object[] unitArray = m.GetCustomAttributes(typeof(StateAttribute), true);
            jState state = new jState(m, StateType.Ordinary,unitArray);
            state.IsFinalState = false;
            state.IsStartState = false;
            return state;
        }
        /// <summary>
        /// Create
        /// </summary>
        /// <param name="m"></param>
        /// <returns></returns>
        internal static jState CreateFinal(MethodInfo m)
        {
            object[] unitArray = m.GetCustomAttributes(typeof(FinalStateAttribute), true);
            jState state = new jState(m, StateType.Final, unitArray);
            state.IsFinalState = true;
            state.IsStartState = false;
            return state;
        }
        /// <summary>
        /// Create
        /// </summary>
        /// <param name="m"></param>
        /// <returns></returns>
        internal static jState CreateStart(MethodInfo m)
        {
            object[] unitArray = m.GetCustomAttributes(typeof(StartStateAttribute), true);
            jState state = new jState(m, StateType.Start, unitArray);
            state.IsFinalState = false;
            state.IsStartState = true;
            return state;
        }
        #endregion
        #region  Constructors
        /// <summary>
        ///  Constructor
        /// </summary>
        /// <param name="method"></param>
        /// <param name="stateType"></param>
        /// <param name="unitArray"></param>
        private jState(MethodInfo method, StateType stateType, object[] unitArray)
        {
            this.StateType = stateType;
            this.Name = method.Name;
            this.MethodInfo = method;
            StateAttribute uiAttr = (StateAttribute)unitArray[0];
            if (uiAttr.TagName != "")
            {
                FieldInfo tagField = method.DeclaringType.GetField(uiAttr.TagName, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.FlattenHierarchy);

                this.Tag = jClass.GetTag(method, uiAttr.TagName, "");
            }
            else this.Tag = "";
        }
        /// <summary>
        /// Constructor
        /// </summary>
        private jState()
        {
        }
        #endregion
        //
        // Instance properties
        //
        #region MethodInfo
        private MethodInfo methodInfo;
        /// <summary>
        /// 
        /// </summary>
        public MethodInfo MethodInfo
        {
            get{return this.methodInfo;}
            private set { this.methodInfo = value; }
        }
        #endregion
        #region Tag
        /// <summary>
        /// 
        /// </summary>
        public object Tag
        {
            get;
            private set;
        }
        #endregion
        #region Name
        /// <summary>
        /// 
        /// </summary>
        public string Name
        {
            get;
            internal set;
        }
        #endregion
        #region StateType
        /// <summary>
        /// 
        /// </summary>
        public StateType StateType
        {
            get;
            internal set;
        }
        #endregion
        #region IsStartState
        /// <summary>
        /// 
        /// </summary>
        public bool IsStartState
        {
            get;
            private set;
        }
        #endregion
        #region IsFinalState
        /// <summary>
        /// 
        /// </summary>
        public bool IsFinalState
        {
            get;
            private set;
        }
        #endregion
        #region IsEmpty
        /// <summary>
        /// 
        /// </summary>
        public bool IsEmpty
        {
            get { return (this.Name == C.EMPTY); }
        }
        #endregion
        //
        // Instance Methods
        //
        #region Execute
        /// <summary>
        /// Execute a State Method.
        /// </summary>
        /// <param name="workflow"></param>
        internal void Execute(jObject workflow)
        {
            MethodInfo.Invoke(workflow, new object[0]);
        }
        #endregion
        #region ToString
        /// <summary>
        /// Provides information for visual studio when hovering over variables.
        /// </summary>
        /// <returns></returns>
        public override string ToString()
        {
            return this.GetType().Name + ":'" + this.Name + "'";
        }
        #endregion
    }
}
